﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SadlandVillage.Core;
using SadlandVillage.Core.Helper;

namespace SadlandVillage.Minigames.River
{
    public enum GameModes
    {
        Beginning,
        Running,
        NewLevel,
        Stopped,
        PlateGameOver,
        GameOver
    }

    public class River : MiniGame
    {
        public override event BackToMapEventHandler GameCancelled;
        private GameModes gameMode;
        private bool firstIntro = true;

        //backgroud
        private Texture2D background;

        
        //boat
        private Sprite boat;
        Vector2 boatPosition;
        Vector2 boatSpeed = new Vector2(1,-0.2f);
        private Color[] boatColor;

        #region Barrels
        //barrel1
        private Sprite barrel1;
        Vector2 barrel1Position;
        Vector2 barrelSpeed = new Vector2(1, 1);
        private Color[] barrelColor;

        //barrel2
        private Sprite barrel2;
        Vector2 barrel2Position;

        //barrel3
        private Sprite barrel3;
        Vector2 barrel3Position;

        //barrel4
        private Sprite barrel4;
        Vector2 barrel4Position;

        //barrel5
        private Sprite barrel5;
        Vector2 barrel5Position;

        //barrel6
        private Sprite barrel6;
        Vector2 barrel6Position;

        //barrel7
        private Sprite barrel7;
        Vector2 barrel7Position;

        //barrel8
        private Sprite barrel8;
        Vector2 barrel8Position;

        //barrel9
        private Sprite barrel9;
        Vector2 barrel9Position;

    #endregion Barrels

        //GameOver
        SpriteFont font;
        Vector2 fontPosition = new Vector2(10, 20);
        

        private int ScreenWidth;
        private int ScreenHeight;

        public River(ContentManager content, SpriteBatch spriteBatch)
            : base(content, spriteBatch)
        {
            this.ScreenHeight = spriteBatch.GraphicsDevice.Viewport.Height;
            this.ScreenWidth = spriteBatch.GraphicsDevice.Viewport.Width;

            GameActivated += new EventHandler(Initialize);
            GameDeactivated += new EventHandler(Finalize);
        }

        private void Initialize(object sender, EventArgs e)
        {
            gameMode = GameModes.Running;
        }

        private void Finalize(object sender, EventArgs e)
        {
            GlobalContainer.components.Clear();
            this.GameCancelled(0);
        }

        public override void UnloadContent()
        {
            //TODO
        }

        public override void LoadContent()
        {
            background = content.Load<Texture2D>(@"Images\map");
            boatPosition = new Vector2(400, 500);
            boat = new Sprite(content.Load<Texture2D>(@"Images\boat"), boatPosition);

            boatColor = new Color[boat.texture.Width * boat.texture.Height];

            boat.texture.GetData(boatColor);

            this.boat.rectangle = new Rectangle((int)boat.position.X, (int)boat.position.Y, boat.texture.Width - 20, boat.texture.Height - 51);

            #region Barrels

            barrel1Position = new Vector2(400, -100);
            barrel1 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel1Position);

            barrel2Position = new Vector2(230, -300);
            barrel2 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel2Position);

            barrel3Position = new Vector2(200, -400);
            barrel3 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel3Position);

            barrel4Position = new Vector2(500, -500);
            barrel4 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel4Position);

            barrel5Position = new Vector2(543, -600);
            barrel5 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel5Position);

            barrel6Position = new Vector2(421, -700);
            barrel6 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel6Position);

            barrel7Position = new Vector2(333, -550);
            barrel7 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel7Position);

            barrel8Position = new Vector2(550, -250);
            barrel8 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel8Position);

            barrel9Position = new Vector2(250, -200);
            barrel9 = new Sprite(content.Load<Texture2D>(@"Images\barrel1"), barrel9Position);

            #endregion Barrels

            font = content.Load<SpriteFont>(@"Fonts\GameFont");
            StaticContainer.gameOverLoose = content.Load<Texture2D>(@"Images\gameOverLoose");
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (Input.KeyPressed(Keys.Escape))
                FinishGame();

            bool left = false;
            bool right = false;
            if (Input.WiimoteConnected)
            {
                left = Input.GetCursor(Input.Cursors.Cursor1).Position.X > boatPosition.X;
                right = Input.GetCursor(Input.Cursors.Cursor1).Position.X < boatPosition.X;
            }
            else
            {
                left = Input.KeyDown(Keys.Left); 
                right = Input.KeyDown(Keys.Right);
            }


            switch (gameMode)
            {
                case GameModes.NewLevel:
                    gameMode = GameModes.Stopped;
                    if (gameMode == GameModes.Stopped)
                    {
                        gameMode = GameModes.Running;
                    }
                    break;

                case GameModes.Running:
                    if (left)
                    {
                        if (boatPosition.X < 610)
                        {
                            boatPosition.X += 3;
                        }

                    }
                    if (right)
                    {
                        if (boatPosition.X > 140)
                        {
                            boatPosition.X += -3;
                        }
                    }
                    this.moveBoat();
                    this.moveBarrels();
                    this.refreshRectangles();
                    if (boatPosition.Y > 100)
                    {
                        this.refreshBarrels();
                    }

                    if (detectColision(boatPosition, barrel1Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel2Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel3Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel4Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel5Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel6Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel7Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel8Position, boat, barrel1, boatColor, barrelColor) ||
                        detectColision(boatPosition, barrel9Position, boat, barrel1, boatColor, barrelColor))
                    {
                        GlobalContainer.components.Add(new GameOver(GlobalContainer.game));
                        gameMode = GameModes.GameOver;
                    }

                    if (boatPosition.Y < -10)
                    {
                        gameMode = GameModes.GameOver;
                    }
                    break;
                case GameModes.Stopped:
                    break;
                case GameModes.GameOver:
                    if (Input.KeyPressed(Keys.Escape))
                    {
                        FinishGame();
                    }

                    if (Input.KeyPressed(Keys.Enter))
                    {
                        gameMode = GameModes.NewLevel;
                    }
                    break;
                default:
                    break;
            }
            
            
        }

        private void FinishGame()
        {
            this.Active = false;
            this.ResetGame();
            GlobalContainer.gameStateManager.SetCurrentState(GameState.MapGameplay);

        }

        private void ResetGame()
        {

        }

        private void refreshBarrels()
        {
            if (barrel1Position.Y == 600)
            {
                barrel1Position.Y = -100;
            }
            if (barrel2Position.Y == 600)
            {
                barrel2Position.Y = -100;
            }
            if (barrel3Position.Y == 600)
            {
                barrel3Position.Y = -100;
            }
            if (barrel4Position.Y == 600)
            {
                barrel4Position.Y = -100;
            }
            if (barrel5Position.Y == 600)
            {
                barrel5Position.Y = -100;
            }
            if (barrel6Position.Y == 600)
            {
                barrel6Position.Y = -100;
            }
            if (barrel7Position.Y == 600)
            {
                barrel7Position.Y = -100;
            }
            if (barrel8Position.Y == 600)
            {
                barrel8Position.Y = -100;
            }
            if (barrel9Position.Y == 600)
            {
                barrel9Position.Y = -100;
            }
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(background, new Vector2(0,0), Color.White);
            spriteBatch.Draw(boat.texture, boatPosition, Color.White);
            spriteBatch.Draw(barrel1.texture, barrel1Position, Color.White);
            spriteBatch.Draw(barrel2.texture, barrel2Position, Color.White);
            spriteBatch.Draw(barrel3.texture, barrel3Position, Color.White);
            spriteBatch.Draw(barrel4.texture, barrel4Position, Color.White);
            spriteBatch.Draw(barrel5.texture, barrel5Position, Color.White);
            spriteBatch.Draw(barrel6.texture, barrel6Position, Color.White);
            spriteBatch.Draw(barrel7.texture, barrel7Position, Color.White);
            spriteBatch.Draw(barrel8.texture, barrel8Position, Color.White);
            spriteBatch.Draw(barrel9.texture, barrel9Position, Color.White);

            spriteBatch.End();
        }

        private void moveBoat()
        {
            boatPosition.Y += boatSpeed.Y;
        }

        private void moveBarrels()
        {
            barrel1Position.Y += barrelSpeed.Y;
            barrel2Position.Y += barrelSpeed.Y;
            barrel3Position.Y += barrelSpeed.Y;
            barrel4Position.Y += barrelSpeed.Y;
            barrel5Position.Y += barrelSpeed.Y;
            barrel6Position.Y += barrelSpeed.Y;
            barrel7Position.Y += barrelSpeed.Y;
            barrel8Position.Y += barrelSpeed.Y;
            barrel9Position.Y += barrelSpeed.Y;
        }

        private void refreshRectangles()
        {
            this.boat.rectangle = new Rectangle((int)boat.position.X, (int)boat.position.Y, boat.texture.Width, boat.texture.Height);
            this.barrel1.rectangle = new Rectangle((int)barrel1.position.X, (int)barrel1.position.Y, barrel1.texture.Width, barrel1.texture.Height);

        }

        private bool detectColision(Vector2 boatPosition, Vector2 barrelPosition, Sprite spriteBoat, Sprite spriteBarrel, Color[] colorSpriteBoat, Color[] colorspriteBarrel)
        {

            float x1 = boatPosition.X;
            float x2 = boatPosition.X + spriteBoat.rectangle.Width - 10;
            float x3 = barrelPosition.X;
            float x4 = barrelPosition.X + spriteBarrel.rectangle.Width - 10;
            float y1 = boatPosition.Y;
            float y2 = boatPosition.Y + spriteBoat.rectangle.Height - 10;
            float y3 = barrelPosition.Y;
            float y4 = barrelPosition.Y + spriteBarrel.rectangle.Height - 10;

            //teste os limites e veja se os objetos colidiram

            bool colided = false;

            if ((((x3 <= x1) && (x1 <= x4)) || ((x3 <= x2) && (x2 <= x4))) && (((y3 <= y1) && (y1 <= y4)) || ((y3 <= y2) && (y2 <= y4))))
            {
                return true;
            }
            return colided;
        }
    }
}
